//
//  ImportantIndurstryDetailViewController.m
//  Utils
//
//  Created by enghou on 2018/6/19.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

#import "ImportantIndurstryDetailViewController.h"
#import "SelectedButton.h"
#import "XXTextView.h"
#import <TZImagePickerController.h>
#import "LocalPersistent.h"
#import "NetWorkerManager.h"
#import "FileItemModel.h"
@interface ImportantIndurstryDetailViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *industryImageView;

@property (weak, nonatomic) IBOutlet UITextField *industryNameField;

@property (weak, nonatomic) IBOutlet UITextField *cardNumField;

@property (weak, nonatomic) IBOutlet UITextField *industryTypeField;

@property(nonatomic,strong)PropertyDictItemModel *typeModel;

@property(nonatomic,strong)NSArray *types;

@property (weak, nonatomic) IBOutlet UITextField *xingzhiField;
@property(nonatomic,strong)PropertyDictItemModel *xingzhiModel;
@property(nonatomic,strong)NSArray *xingzhis;

@property (weak, nonatomic) IBOutlet UITextField *politicField;

@property (weak, nonatomic) IBOutlet UITextField *chipField;

@property (weak, nonatomic) IBOutlet UITextField *employeeCountField;

@property (weak, nonatomic) IBOutlet UITextField *enterpriseTelField;

@property (weak, nonatomic) IBOutlet UITextField *enterpriseFaxField;

@property (weak, nonatomic) IBOutlet UITextField *manipulatorField;
@property (weak, nonatomic) IBOutlet UITextField *manipulatorTelField;
@property (weak, nonatomic) IBOutlet UITextField *manipulatorMobileField;
@property (weak, nonatomic) IBOutlet UITextField *addressField;

@property (weak, nonatomic) IBOutlet UITextField *concernedField;
@property(nonatomic,strong)PropertyDictItemModel *concernModel;
@property(nonatomic,strong)NSArray *concerns;

@property (weak, nonatomic) IBOutlet SelectedButton *yesBtn;

@property (weak, nonatomic) IBOutlet SelectedButton *noBtn;

@property(nonatomic,assign)BOOL ifHasProblem;

@property (strong, nonatomic) IBOutlet UITableViewCell *problemSituationCell;

@property (strong, nonatomic) IBOutlet UITableViewCell *problemHandlingSituationCell;

@property (weak, nonatomic) IBOutlet XXTextView *problemSituationField;

@property (weak, nonatomic) IBOutlet XXTextView *problemHandlingSituationField;

@property (weak, nonatomic) IBOutlet XXTextView *noteField;

@property(nonatomic,copy)NSString *filePath;

@end

@implementation ImportantIndurstryDetailViewController

-(instancetype)init{
    UIStoryboard *story = [UIStoryboard storyboardWithName:@"ImportantIndurstry" bundle:[NSBundle mainBundle]];
    return [story instantiateViewControllerWithIdentifier:@"ImportantIndurstryDetailViewController"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

#pragma mark - 获取详情的必备方法
-(NSDictionary *)detailParam{
    return @{@"id":self.ids};
}

-(NSString *)detailPath{
    return @"/mobile/comprehensiveGovernance/keyIndustry/getKeyIndustryById.action";
}

-(void(^)(NSDictionary *))configView{
    void(^config)(NSDictionary *) = ^(NSDictionary *source){
        NSDictionary *data = source[@"data"];
        NSString *imageUrl = data[@"imgUrl"];
        if (imageUrl) {
            [self.industryImageView sd_setImageWithURL:[NSURL URLWithString:imageUrl]];
        }
        self.industryNameField.text = data[@"name"];
        self.cardNumField.text = data[@"licenseNumber"];
        self.typeModel = [[PropertyDictItemModel alloc]init];
        self.typeModel.ids = data[@"type"][@"id"];
        self.industryTypeField.text = data[@"type"][@"displayName"];
        self.xingzhiModel = [[PropertyDictItemModel alloc]init];
        self.xingzhiModel.ids = data[@"quality"][@"id"];
        self.xingzhiField.text = data[@"quality"][@"displayName"];
        self.politicField.text = data[@"nativePoliceStation"];
        self.chipField.text = [NSString stringWithFormat:@"%li",[data[@"registeredCapital"] integerValue]];
        self.employeeCountField.text = [NSString stringWithFormat:@"%li",[data[@"employeesNumber"] integerValue]];
        self.enterpriseTelField.text = data[@"telephone"];
        self.enterpriseFaxField.text = data[@"fax"];
        self.manipulatorField.text = data[@"legalRepresentative"];
        self.manipulatorTelField.text = data[@"legalPersonPhone"];
        self.manipulatorMobileField.text = data[@"mobileNumber"];
        self.addressField.text = data[@"address"];
        self.concernModel = [[PropertyDictItemModel alloc]init];
        self.concernModel.ids = data[@"attentionExtent"][@"id"];
        self.concernedField.text = data[@"attentionExtent"][@"displayName"];
        BOOL ifHasProblem = [data[@"hasDanger"] boolValue];
        if (ifHasProblem) {
            self.yesBtn.selected = YES;
            self.noBtn.selected = NO;
            self.problemHandlingSituationCell.contentView.hidden = NO;
            self.problemHandlingSituationCell.contentView.hidden = NO;
        }else{
            self.yesBtn.selected = NO;
            self.noBtn.selected = YES;
            self.problemHandlingSituationCell.contentView.hidden = YES;
            self.problemHandlingSituationCell.contentView.hidden = YES;
        }
        self.ifHasProblem = YES;
        self.problemSituationField.text = data[@"dangerCase"];
        self.problemHandlingSituationField.text = data[@"dangerReformCase"];
        self.noteField.text = data[@"remark"];
        id orgId = data[@"organization"][@"id"];
        self.orgId = orgId;
        [self.tableView reloadData];
    };
    return config;
}

#pragma mark - 新增的方法
-(NSDictionary *)addedParameter{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    if (self.ids) {
        param[@"keyIndustry.id"] = self.ids;
    }
    if (self.industryNameField.text.length == 0) {
        [self alertSome:@"请输入重点行业名称"];
        return nil;
    }
    param[@"keyIndustry.name"] = self.industryNameField.text;
    param[@"keyIndustry.licenseNumber"] = self.cardNumField.text;
    if (self.typeModel.ids.length == 0) {
        [self alertSome:@"请选择行业类别!"];
        return nil;
    }
    param[@"keyIndustry.industryType.id"] = self.typeModel.ids;
    if (self.xingzhiModel.ids.length > 0) {
        param[@"keyIndustry.quality.id"] = self.xingzhiModel.ids;
    }
    if (self.politicField.text.length == 0) {
        [self alertSome:@"请输入所属派出所!"];
        return nil;
    }
    if (self.orgId) {
        param[@"keyIndustry.organization.id"] = self.orgId;
    }else{
        NSString *orgId = [[LocalPersistent sharedInstance]contentForKey:@"orgId"];
        if (orgId) {
            param[@"keyIndustry.organization.id"] = orgId;
        }else{
            [self alertSome:@"必须选择网格ID"];
            return nil;
        }
    }
    param[@"keyIndustry.nativePoliceStation"] = self.politicField.text;
    param[@"keyIndustry.registeredCapital"] = self.chipField.text;
    param[@"keyIndustry.employeesNumber"] = self.employeeCountField.text;
    param[@"keyIndustry.telephone"] = self.enterpriseTelField.text;
    param[@"keyIndustry.faxTelephone"] = self.enterpriseFaxField.text;
    param[@"keyIndustry.legalRepresentative"] = self.manipulatorField.text;
    param[@"keyIndustry.legalPersonPhone"] = self.manipulatorTelField.text;
    param[@"keyIndustry.mobileNumber"] = self.manipulatorMobileField.text;
    param[@"keyIndustry.address"] = self.addressField.text;
    if (self.concernModel.ids.length > 0) {
        param[@"keyIndustry.attentionExtent.id"] = self.concernModel.ids;
    }
    if (_yesBtn.selected) {
        param[@"keyIndustry.hasDanger"] = @"true";
        param[@"keyIndustry.dangerCase"] = self.problemSituationField.text;
        param[@"keyIndustry.dangerReformCase"] = self.problemHandlingSituationField.text;
        param[@"keyIndustry.remark"] = self.noteField.text;
    }else{
        param[@"keyIndustry.hasDanger"] = @"false";
        param[@"keyIndustry.remark"] = self.noteField.text;
    }
    if (self.filePath) {
        param[@"file.path"] = self.filePath;
    }
    return param;
}

-(void)uploadFiles:(NSArray <NSData *>*)files successBlock:(void(^)(NSArray <FileItemModel *>*))suc{
    [self beginLoading];
    [[NetWorkerManager sharedInstance]uploadImageWithPath:kUploadFilePath parameter:nil files:files fileName:@"file" suffix:@"jpg" completeBlock:^(NSDictionary *source) {
        [self endLoading];
        BaseModel *model = [[BaseModel alloc]initWithSourceDict:source listItemClass:[FileItemModel class]];
        if (model.success) {
            if (suc) {
                suc(model.list);
            }
        }else{
            [self alertSome:model.msg];
        }
    } failBlock:^(NSInteger code) {
        [self endLoading];
        [self alertSome:kNetWorkFail];
    }];
}
-(void)addRecord{
    if (self.industryImageView.image) {
        NSData *data = UIImageJPEGRepresentation(self.industryImageView.image, 0.1);
        [self uploadFiles:@[data] successBlock:^(NSArray<FileItemModel *> *files) {
            FileItemModel *file = [files objectAtIndex:0];
            self.filePath = file.path;
            [super addRecord];
        }];
    }else{
        [super addRecord];
    }
}

-(NSString *)addPath{
    return @"/mobile/comprehensiveGovernance/keyIndustry/addKeyIndustry.action";
}

#pragma mark - 更新的方法
-(NSDictionary *)updateParameter{
    return [self addedParameter];
}

-(NSString *)updatePath{
    return @"/mobile/comprehensiveGovernance/keyIndustry/updateKeyIndustry.action";
}

-(void)updateRecord{
    if (self.industryImageView.image) {
        NSData *data = UIImageJPEGRepresentation(self.industryImageView.image, 0.1);
        [self uploadFiles:@[data] successBlock:^(NSArray<FileItemModel *> *files) {
            FileItemModel *file = [files objectAtIndex:0];
            self.filePath = file.path;
            [super updateRecord];
        }];
    }else{
        [super updateRecord];
    }
}

#pragma mark - 删除的回调
-(NSDictionary *)delParameter{
    return @{@"id":self.ids};
}

-(NSString *)delPath{
    return @"/mobile/comprehensiveGovernance/keyIndustry/deleteKeyIndustryById.action";
}

-(NSString *)refresh_not_name{
    return @"refresh_indurstry";
}




- (IBAction)yesAction:(UIButton *)sender {
    sender.selected = YES;
    self.noBtn.selected = NO;
    self.ifHasProblem = YES;
    self.problemHandlingSituationCell.contentView.hidden = NO;
    self.problemHandlingSituationCell.contentView.hidden = NO;
    [self.tableView reloadData];
}

- (IBAction)noAction:(UIButton *)sender {
    sender.selected = YES;
    self.yesBtn.selected = NO;
    self.ifHasProblem = NO;
    self.problemHandlingSituationCell.contentView.hidden = YES;
    self.problemHandlingSituationCell.contentView.hidden = YES;
    [self.tableView reloadData];
}

-(void)pickImage{
    TZImagePickerController *picker = [[TZImagePickerController alloc]initWithMaxImagesCount:1 delegate:nil];
    picker.didFinishPickingPhotosHandle = ^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        self.industryImageView.image = photos[0];
    };
    [self presentVC:picker];
}

-(void)chooseType{
    [self commonAlertWithContent:@"行业类别" choosenBlock:^(PropertyDictItemModel *model) {
        self.typeModel = model;
        self.industryTypeField.text = self.typeModel.displayName;
    } getList:^(NSArray *array) {
        self.types = array;
    }];
}

-(void)chooseQuality{
    [self commonAlertWithContent:@"行业性质" choosenBlock:^(PropertyDictItemModel *model) {
        self.xingzhiModel = model;
        self.xingzhiField.text = model.displayName;
    } getList:^(NSArray *array) {
        self.xingzhis = array;
    }];
}

-(void)chooseConcern{
    [self commonAlertWithContent:@"关注程度" choosenBlock:^(PropertyDictItemModel *model) {
        self.concernModel = model;
        self.concernedField.text = model.displayName;
    } getList:^(NSArray *array) {
        self.concerns = array;
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
        case 0:
            [self pickImage];
            break;
        case 3:
            [self chooseType];
            break;
        case 4:
            [self chooseQuality];
            break;
        case 14:
            [self chooseConcern];
            break;
        default:
            break;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
        case 16:
        case 17:
        {
            if (self.ifHasProblem) {
                return [super tableView:tableView heightForRowAtIndexPath:indexPath];
            }else{
                return 0.0;
            }
        }
            break;
        default:
            return [super tableView:tableView heightForRowAtIndexPath:indexPath];
            break;
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
